//
//  WDCalendarMonthCollectionView.m
//  teatCalendar
//
//  Created by 解向前 on 2018/3/6.
//  Copyright © 2018年 com.test. All rights reserved.
//

#import "WDCalendarMonthCollectionView.h"
#import "WDCalendarPageCollectionView.h"
#import "WDCalendarMonthCell.h"
#import "WDCanlendarHandler.h"
#import "WDCalendarMonthModel.h"



static NSString *wDCalendarMonthCellID = @"WDCalendarMonthCellID";
@interface WDCalendarMonthCollectionView()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) NSArray *months;

@end
@implementation WDCalendarMonthCollectionView

- (instancetype)initWithFrame:(CGRect)frame {
    UICollectionViewFlowLayout *flowLayout = ({
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = frame.size;
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout;
    });
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    if (self) {
        [self setupInit];
    }
    return self;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    WDCalendarMonthCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:wDCalendarMonthCellID forIndexPath:indexPath];
    WDCalendarMonthModel *model = self.months[indexPath.item];
    cell.calendarPageView.models = model.dayModels;
    NSLog(@"indexPath: %@", indexPath);
    switch (indexPath.item) {
        case 0:
            cell.calendarPageView.backgroundColor = [UIColor redColor];
            break;
        case 1:
            cell.calendarPageView.backgroundColor = [UIColor blueColor];
            break;
        case 2:
            cell.calendarPageView.backgroundColor = [UIColor greenColor];
            break;
            
        default:
            cell.calendarPageView.backgroundColor = [UIColor whiteColor];
            break;
    }
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.months.count;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    WDCanlendarHandler *handler = [WDCanlendarHandler sharedInstance];
    CGFloat w = scrollView.contentSize.width / self.months.count;
    if (scrollView.contentOffset.x == 0) {
        self.months = [handler getAllMonthsForLastPage:self.months];
    }
    else if (scrollView.contentOffset.x == w) {
        
    }
    else if (scrollView.contentOffset.x == w * 2) {
        self.months = [handler getAllMonthsForNextPage:self.months];
    }
    
}

- (void)setupInit {
    self.backgroundColor = [UIColor whiteColor];
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.pagingEnabled = YES;
    self.bounces = NO;
    [self registerClass:[WDCalendarMonthCell class] forCellWithReuseIdentifier:wDCalendarMonthCellID];
    self.months = [[WDCanlendarHandler sharedInstance] getAllMonths:[NSDate date]];
    self.dataSource = self;
    self.delegate = self;
}
- (void)setMonths:(NSArray *)months {
    _months = months;
    [self reloadData];
    if (self.monthChange) {
        WDCalendarMonthModel *model = months[1];
        self.monthChange(model.pageMonth);
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];
    NSLog(@"创建的indexPath: %@", indexPath);
    [self scrollToItemAtIndexPath: indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
}
@end
